home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Macintosh Drag and Drop / Demo Applications / Dragster / DragText Sources / teutilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  4.2 KB  |  174 lines  |  [TEXT/KAHL]

  1. /*
  2.  *
  3.  *        teutilities.c
  4.  *
  5.  *        Supplemental TextEdit utilities.
  6.  *        
  7.  *
  8.  *        Author:        Rob Johnston
  9.  *        Date:        Monday, February 3, 1992
  10.  *
  11.  *        12/30/94    JML        Code now compiles with Universal interfaces.
  12.  *
  13.  *        Copyright © 1992 Apple Computer, Inc.
  14.  *
  15.  */
  16.  
  17. #include <TextEdit.h>
  18. #include <TextUtils.h>
  19. #include <Scrap.h>
  20. #include <Script.h>
  21.  
  22.  
  23. /*
  24.  *    TEICut is an intelligent version of TECut. If the selection is a range
  25.  *    of words, TEICut cuts the selection and removes a space from either the
  26.  *    beginning or end of the selection point when the result of cutting leaves
  27.  *    one too many spaces in a sentence. The resulting change in size of the
  28.  *    TextEdit record is returned.
  29.  *
  30.  *    NOTE: This function does not work properly in all situations. It may still
  31.  *        need some work.
  32.  */
  33.  
  34. short TEICut(TEHandle theTE)
  35.  
  36. {    OffsetTable        startOffsets, endOffsets;
  37.     short            selStart, selEnd, characters;
  38.     Handle            theScrap;
  39.  
  40.     if (!(characters = (**theTE).selStart - (**theTE).selEnd))
  41.         return(0);
  42.  
  43.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, true,
  44.                     0L, startOffsets);
  45.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, false,
  46.                     0L, endOffsets);
  47.  
  48.     if ((startOffsets[0].offFirst == (**theTE).selStart) &&
  49.         (endOffsets[0].offSecond  == (**theTE).selEnd)) {
  50.  
  51.         /*
  52.          *    Both the beginning and end of the current selection is on
  53.          *    word boundaries.
  54.          */
  55.  
  56.         selStart = (**theTE).selStart;
  57.         selEnd = (**theTE).selEnd;
  58.         if ((*((**theTE).hText))[selStart - 1] == ' ') {
  59.             TESetSelect(selStart - 1, selEnd, theTE);
  60.             TECut(theTE);
  61.             theScrap = TEScrapHandle();
  62.             BlockMove((char *) (*theScrap) + 1, (*theScrap), TEGetScrapLen() - 1);
  63.             TESetScrapLen(TEGetScrapLen() - 1);
  64.             ZeroScrap();
  65.             TEToScrap();
  66.             characters--;
  67.         } else if ((*((**theTE).hText))[selEnd] == ' ') {
  68.             TESetSelect(selStart, selEnd + 1, theTE);
  69.             TECut(theTE);
  70.             TESetScrapLen(TEGetScrapLen() - 1);
  71.             ZeroScrap();
  72.             TEToScrap();
  73.             characters--;
  74.         } else {
  75.             TECut(theTE);
  76.         }
  77.     } else {
  78.         TECut(theTE);
  79.     }
  80.     return(characters);
  81. }
  82.  
  83.  
  84. /*
  85.  *    TEIPaste is an intelligent version of TEPaste. When pasting into a sentence,
  86.  *    an extra space may be added to either the start or end of the insertion
  87.  *    to maintain readibility of the resulting sentence. Optionally, pointers
  88.  *    may be provided to spaceBefore and/or spaceAfter. These flags return
  89.  *    true if a space was inserted before or after the insertion respectively.
  90.  *
  91.  *    NOTE: This function does not always work properly. It still needs work.
  92.  */
  93.  
  94. short TEIPaste(TEHandle theTE, short *spaceBefore, short *spaceAfter)
  95.  
  96. {    OffsetTable        startOffsets, endOffsets;
  97.     short            addSpaceAfter, characters;
  98.  
  99.     characters = (**theTE).selStart - (**theTE).selEnd;
  100.  
  101.     if (spaceBefore)
  102.         *spaceBefore = false;
  103.     if (spaceAfter)
  104.         *spaceAfter  = false;
  105.  
  106.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selStart, false,
  107.                     0L, startOffsets);
  108.     FindWord(*((**theTE).hText), (**theTE).teLength, (**theTE).selEnd, true,
  109.                     0L, endOffsets);
  110.  
  111.     addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) &&
  112.                      ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
  113.  
  114.     if ((startOffsets[0].offSecond == (**theTE).selStart) &&
  115.             ((*((**theTE).hText))[(**theTE).selStart - 1] != ' ')) {
  116.         TEKey(' ', theTE);
  117.         characters++;
  118.         if (spaceBefore)
  119.             *spaceBefore = true;
  120.     }
  121.  
  122.     TEPaste(theTE);
  123.     characters += TEGetScrapLen();
  124.  
  125.     if (addSpaceAfter) {
  126.         TEKey(' ', theTE);
  127.         characters++;
  128.         if (spaceAfter)
  129.             *spaceAfter = true;
  130.     }
  131.     return(characters);
  132. }
  133.  
  134.  
  135. /*
  136.  *    TEIsFrontOfLine, given an offset and a TextEdit handle, returns true if
  137.  *    the given offset is at the beginning of a line start.
  138.  */
  139.  
  140. short TEIsFrontOfLine(short offset, TEHandle theTE)
  141.  
  142. {    short        line = 0;
  143.  
  144.     if ((**theTE).teLength == 0)
  145.         return(true);
  146.  
  147.     if (offset >= (**theTE).teLength)
  148.         return( (*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d );
  149.  
  150.     while ((**theTE).lineStarts[line] < offset)
  151.         line++;
  152.  
  153.     return( (**theTE).lineStarts[line] == offset );
  154. }
  155.  
  156.  
  157. /*
  158.  *    TEGetLine, given an offset and a TextEdit handle, returns the line number
  159.  *    of the line that contains the offset.
  160.  */
  161.  
  162. short TEGetLine(short offset, TEHandle theTE)
  163.  
  164. {    short        line = 0;
  165.  
  166.     if (offset > (**theTE).teLength)
  167.         return((**theTE).nLines);
  168.  
  169.     while ((**theTE).lineStarts[line] < offset)
  170.         line++;
  171.     
  172.     return(line);
  173. }
  174.